/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function ($, common, ready, Collection, template, Model, dialog) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'goods_rebate_list',
                model: {
                    url: _global.url.api + 'goods_rebate',
                    event: {
                        '.item-btn-reset:click': 'reset',
                    },
                    reset: function (e) {
                        var __this = {};
                        __this.data = this.data;
                        e.preventDefault();

                        dialog({
                            'content': '确定恢复并删除此商品吗?',
                            ok: function () {
                                if (new Model({
                                        url: _global.url.api + 'goods_rebate',
                                        data: {goods_ids: __this.data.goods_id, act: 'reset'}
                                    }).post()) {
                                    _this.collection.reload();
                                }
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-set').on('click', function (e) {
                var data = {};
                data.goods_ids = $("[name=goods_ids]").val();
                data.rebate = $("[name=rebate]").val();

                if (data.goods_ids == '' || data.rebate == '') {
                    Alert({msg: '请填写相关信息', type: 'error'});
                    return false;
                }

                if (new Model({
                        url: _global.url.api + 'goods_rebate',
                        data: data
                    }).post()) {
                    _this.collection.reload();
                }
            });

        },
    };

    new index();
});